{% extends "base.html" %}
{% load i18n %}
{% block content %}


<div class="page-header" v-pre>
  <h1>{{user.username}} ({{user.email}})</h1>
</div>

<div class="panel panel-primary" v-pre>
  <div class="panel-heading">API Token</div>
  <table class="table">
    <tr>
      <th>Token</th>
      <th>Actions</th>
    </tr>
    <tr>
      {% if api_token %}
      <td>
          {{ api_token }}
      </td>
      <td>
        <form
          method="get"
          action="{% url 'token-generate' %}"
          style="display: inline"
          onsubmit="return confirm('{% trans 'Are you sure? The current token will become invalid.' %}')"
        >
          {% csrf_token %}
          <button class="btn btn-primary btn-xs">
            Regenerate Token
          </button>
        </form>
        <form
          method="get"
          action="{% url 'token-delete' %}"
          style="display: inline"
          onsubmit="return confirm('{% trans 'Are you sure?' %}')"
        >
          {% csrf_token %}
          <button class="btn btn-danger btn-xs">
            Delete Token
          </button>
        </form>
      </td>
      {% else %}
      <td>
      </td>
      <td>
      <form method="get" action="{% url 'token-generate' %}">
        {% csrf_token %}
        <button class="btn btn-primary btn-xs">
          Generate Token
        </button>
      </form>
      </td>
      {% endif %}

    </tr>
  </table>
</div>

<div class="panel panel-primary" v-pre>
  <div class="panel-heading">Subscriptions</div>
  <table class="table">
    <tr>
      <th>&nbsp;</th>
      <th>Filters</th>
      <th>Actions</th>
    </tr>
    {% for notifier in subscriptions %}
    <tr>
      <td><a href="{{notifier.content_object.get_absolute_url}}">{{notifier.content_object}}</td>
      <td>
        {% for f in notifier.filter_set.all %}
        <span class="label label-primary" style="display: inline-block;">
          {{f.name}}:{{f.value}}
        </span>
        {% endfor %}
      </td>
      <td>
        <a href="{% url 'notifier-edit' notifier.id %}" class="btn btn-warning btn-xs">{% trans "Edit" %}</a>
        <form method="post" action="{% url 'notifier-test' notifier.id %}" style="display: inline">
          {% csrf_token %}
          <input name="next" type="hidden" value="{{ request.get_full_path }}" />
          <button class="btn btn-info btn-xs">{% trans "Test" %}</button>
        </form>
        <form method="post" action="{% url 'notifier-delete' notifier.id %}" onsubmit="return confirm('{% trans "Delete notification?" %}')" style="display: inline">
          {% csrf_token %}
          <input name="next" type="hidden" value="{{ request.get_full_path }}" />
          <button class="btn btn-danger btn-xs">{% trans "Delete" %}</button>
        </form>
      </td>
    </tr>
    {% empty %}
    <tr>
      <td colspan="3">No Subscriptions <a href="{% url 'datasource-list' %}">See Services List</a></td>
    </tr>
    {% endfor %}
  </table>
</div>

<div class="panel panel-primary">
  <div class="panel-heading">Notifications</div>
  {% include "promgen/notifier_block.html" with object=notifiers show_edit=1 %}
  <div class="panel-body" v-pre>
    <div class="row">
      <div class="col-md-3">
        <ul class="nav nav-pills nav-stacked" role="tablist">
          {% for k, v in view.model.driver_set|dictsort:0 %}
          {% if k != "promgen.notification.user" %}
          <li role="presentation">
            <a href="#{{ k|slugify }}" aria-controls="{{ k|slugify }}" role="tab" data-toggle="tab">{{ k }}</a>
          </li>
          {% endif %}
          {% endfor %}
        </ul>
      </div>

      <div class="col-md-8 tab-content">
        {% for k, v in view.model.driver_set|dictsort:0 %}
        {% if k != "promgen.notification.user" %}
        <div role="tabpanel" class="tab-pane panel panel-default" id="{{ k|slugify }}">
          <form action="" method="post">{% csrf_token %}
            <input type="hidden" name="sender" value="{{k}}" />
            <div class="panel-body">
              <pre class="help-block">{{ v.help|urlize }}</pre>
            </div>
            <table class="table">
              {{ v.form.as_table }}
            </table>
            <div class="panel-footer">
              <button class="btn btn-primary">Register Notifier</button>
            </div>
          </form>
        </div>
        {% endif %}
        {% endfor %}
      </div>
    </div>
  </div>
</div>

{% if user.is_staff %}
<div class="panel panel-warning" v-pre>
  <div class="panel-heading">Debug</div>
  <table class="table">
    <tr>
      <td>Discovery Plugins</td>
      <td>
        <ul>
          {% for plugin in discovery_plugins|dictsort:"name" %}
          <li>{{ plugin.name }} - {{ plugin.module_name }}</li>
          {% endfor %}
      </td>
    </tr>
    <tr>
      <td>Notifier Plugins</td>
      <td>
        <ul>
          {% for notifier in notifier_plugins|dictsort:"name" %}
          <li>{{ notifier.name }} - {{ notifier.module_name }}</li>
          {% endfor %}
      </td>
    </tr>
  </table>
</div>
{% endif %}


{% endblock %}
